package cn.lingyangwl.agile.system;

import cn.lingyangwl.agile.model.enums.*;
import cn.lingyangwl.agile.model.module.auth.*;
import cn.lingyangwl.agile.model.module.config.*;
import cn.lingyangwl.agile.system.model.entity.*;
import cn.lingyangwl.agile.system.model.rqrs.accesskey.*;
import cn.lingyangwl.agile.system.model.rqrs.config.*;
import cn.lingyangwl.agile.system.model.rqrs.log.*;
import cn.lingyangwl.agile.system.model.rqrs.tenant.*;
import cn.lingyangwl.agile.system.model.rqrs.user.*;
import cn.lingyangwl.framework.core.response.*;
import cn.lingyangwl.framework.tool.core.exception.*;
import com.alibaba.cloud.nacos.NacosServiceAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author shenguangyang
 */
@Lazy
@Service
@ConditionalOnClass({FeignAutoConfiguration.class, NacosServiceAutoConfiguration.class})
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
public class SystemApiCloudImpl implements SystemApi {
    @Resource
    private RemoteSystemService service;

    @Override
    public AccessKeyResp getAccessKeyBySecretId(String secretId) {
        Resp<AccessKeyResp> resp = service.getAccessKeyBySecretId(secretId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public AccessKeyResp getAccessKeyById(Long id) {
        Resp<AccessKeyResp> resp = service.getAccessKeyById(id);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public LoginUser getLoginUser(LoginUserGetReq req) {
        Resp<LoginUser> resp = service.getLoginUser(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public Long saveUser(UserSaveReq req) {
        Resp<Long> resp = service.saveUser(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public List<UserResp> listUser(UserPageReq query) {
        Resp<List<UserResp>> resp = service.listUser(query);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public List<UserResp> listUserByIds(List<Long> userIds) {
        Resp<List<UserResp>> resp = service.listUserByIds(userIds);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void removeUserById(Long userId) {
        Resp<?> resp = service.removeUserById(userId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public UserResp getUserById(Long userId) {
        Resp<UserResp> resp = service.getUserById(userId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void deleteLoginUser(LoginUserDeleteReq req) {
        Resp<?> resp = service.deleteLoginUser(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public String saveTenant(TenantSaveReq req) {
        Resp<String> resp = service.saveTenant(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public void deleteTenant(String tenantId) {
        Resp<?> resp = service.deleteTenant(tenantId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public void updateTenant(TenantUpdateReq req) {
        Resp<?> resp = service.updateTenant(req);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public void initTenant() {
        Resp<?> resp = service.initTenant();
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
    }

    @Override
    public String getMgtSceneTenantId(String tenantId) {
        Resp<String> resp = service.getMgtSceneTenantId(tenantId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public String getMainTenantId(String tenantId) {
        Resp<String> resp = service.getMainTenantId(tenantId);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public ConfigResp getConfig(ConfigKeyEnum configKey) {
        throw new UnsupportedOperationException("尚未实现远程调用");
    }

    @Override
    public ConfigInfo getUseConfig(ConfigDefineEnum configDefine) {
        Resp<ConfigInfo> resp = service.getUseConfig(configDefine.name());
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public Map<String, List<String>> listAssignRoleUrl(List<Long> roleIds) {
        Resp<Map<String, List<String>>> resp = service.listAssignRoleUrl(roleIds);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public SysUser getUserByAccount(String account) {
        Resp<SysUser> resp = service.getUserByAccount(account);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public List<SysArea> listAreaByNames(List<String> areaTypes, List<String> areaNames) {
        Resp<List<SysArea>> resp = service.listAreaByNames(areaTypes, areaNames);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public List<TenantResp> listTenantByIds(List<String> tenantIds) {
        Resp<List<TenantResp>> resp = service.listTenantByIds(tenantIds);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public List<SysTenant> pageTenant(Integer pageNum, Integer pageSize) {
        Resp<List<SysTenant>> resp = service.pageTenant(pageNum, pageSize);
        RetOps.of(resp).assertSuccess(e -> new BizException(e.getCode(), e.getMsg()));
        return resp.getData();
    }

    @Override
    public int deleteAccessLog(String endTime) {
        Resp<Integer> resp = service.deleteAccessLog(endTime);
        return resp.getData();
    }
}
